import json
import base64
import sys
from datetime import datetime, timedelta
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QLabel,
                             QLineEdit, QPushButton, QSpinBox, QTextEdit,
                             QHBoxLayout, QGroupBox)


class LicenseGenerator(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("无人机照片应用软件-许可证生成器")
        self.setFixedSize(600, 450)
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()

        # 应用信息
        app_group = QGroupBox("应用信息")
        app_layout = QVBoxLayout()
        app_layout.addWidget(QLabel("应用名称:"))
        self.app_input = QLineEdit("无人机照片应用软件")
        app_layout.addWidget(self.app_input)
        app_group.setLayout(app_layout)
        layout.addWidget(app_group)

        # 硬件信息
        hw_group = QGroupBox("硬件信息")
        hw_layout = QVBoxLayout()
        hw_layout.addWidget(QLabel("客户硬件ID:"))
        self.hw_input = QLineEdit()
        hw_layout.addWidget(self.hw_input)
        hw_group.setLayout(hw_layout)
        layout.addWidget(hw_group)

        # 有效期设置
        expiry_group = QGroupBox("有效期设置")
        expiry_layout = QVBoxLayout()
        expiry_layout.addWidget(QLabel("有效期天数:"))
        self.days_input = QSpinBox()
        self.days_input.setRange(1, 365 * 3)  # 1天到3年
        self.days_input.setValue(30)
        expiry_layout.addWidget(self.days_input)
        expiry_group.setLayout(expiry_layout)
        layout.addWidget(expiry_group)

        # 生成结果
        result_group = QGroupBox("生成结果")
        result_layout = QVBoxLayout()
        self.result_output = QTextEdit()
        self.result_output.setReadOnly(True)
        result_layout.addWidget(self.result_output)
        result_group.setLayout(result_layout)
        layout.addWidget(result_group)

        # 操作按钮
        btn_layout = QHBoxLayout()
        self.generate_btn = QPushButton("生成注册码")
        self.generate_btn.clicked.connect(self.generate)
        self.copy_btn = QPushButton("复制到剪贴板")
        self.copy_btn.clicked.connect(self.copy_code)
        btn_layout.addWidget(self.generate_btn)
        btn_layout.addWidget(self.copy_btn)
        layout.addLayout(btn_layout)

        self.setLayout(layout)

    def generate(self):
        """生成注册码"""
        app_name = self.app_input.text().strip()
        hw_id = self.hw_input.text().strip()
        days = self.days_input.value()

        if not all([app_name, hw_id]):
            self.result_output.setPlainText("错误: 请填写完整信息")
            return

        expiry = (datetime.now() + timedelta(days=days)).strftime("%Y-%m-%d")

        license_data = {
            "app": app_name,
            "hid": hw_id,
            "expiry": expiry
        }

        license_code = base64.b64encode(
            json.dumps(license_data).encode()
        ).decode()

        # 显示结果
        self.result_output.setPlainText(
            f"【注册码】\n{license_code}\n\n"
            f"【详细信息】\n"
            f"应用名称: {app_name}\n"
            f"硬件ID: {hw_id}\n"
            f"有效期至: {expiry}\n"
            f"生成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
        )

    def copy_code(self):
        """复制注册码"""
        text = self.result_output.toPlainText()
        if "【注册码】" in text:
            code = text.split("【注册码】")[1].split("【详细信息】")[0].strip()
            QApplication.clipboard().setText(code)
            self.result_output.append("\n\n(已复制到剪贴板)")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    generator = LicenseGenerator()
    generator.show()
    sys.exit(app.exec_())